import { createRouter, createWebHistory } from 'vue-router'
import HomeView from '../views/HomeView.vue'
import AboutView from '../views/AboutView.vue'
import MyHomeView from '../views/MyHomeView.vue'
import MessagesView from '../views/MessagesView.vue'
import ProfileView from '../views/ProfileView.vue'


const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/',
      name: 'home',
      component: HomeView
    },
    {
      path: '/about',
      name: 'about',
      component: AboutView
    },
    {
      path: '/my-home',
      name: 'my-home',
      component: MyHomeView
    },
    {
      path: '/messages',
      name: 'messages',
      component: MessagesView
    },
    {
      path: '/profile',
      name: 'profile',
      component: ProfileView
    },
    {
      path: '/login',
      name: 'login',
      component: () => import('../views/Login.vue')
    },
    {
      path: '/register',
      name: 'register',
      component: () => import('../views/Register.vue')
    },
    {
      path: '/settings',
      name: 'settings',
      component: () => import('../views/SettingsView.vue'),
      meta: { requiresAuth: true }
    },
    {
      path: '/house-detail',
      name: 'house-detail',
      component: () => import('../views/HouseDetail.vue')
    },
    {
      path: '/map',
      name: 'map',
      component: () => import('../views/MapView.vue')
    },
    {
      path: '/house-search',
      name: 'house-search',
      component: () => import('../views/HouseSearch.vue')
    },
    {
      path: '/favorites',
      name: 'favorites',
      component: () => import('../views/FavoritesView.vue'),
      meta: { requiresAuth: true }
    },
    {
      path: '/transactionOne',
      name: 'TransactionOne',
      component: () => import('../views/TWOPage/TransactionOne.vue'),
      meta: { requiresAuth: true }
    },
    {
      path: '/busTwo',
      name: 'BusTwo',
      component: () => import('../views/TWOPage/BusTwo.vue'),
      meta: { requiresAuth: true }
    },
    {
      path: '/housingestateThree',
      name: 'HousingestateThree',
      component: () => import('../views/TWOPage/HousingestateThree.vue'),
      meta: { requiresAuth: true }
    },
    {
      path: '/schoolFour',
      name: 'SchoolFour',
      component: () => import('../views/TWOPage/SchoolFour.vue'),
      meta: { requiresAuth: true }
    },
    {
      path: '/flathuntingFive',
      name: 'FlathuntingFive',
      component: () => import('../views/TWOPage/FlathuntingFive.vue'),
      meta: { requiresAuth: true }
    },
    {
      path: '/order',
      name: 'Order',
      component: () => import('../views/Order.vue'),
      meta: { requiresAuth: true }
    },
    {
      path: '/contract',
      name: 'Contract',
      component: () => import('../views/Contract.vue'),
      meta: { requiresAuth: true }
    },
      {
      path: '/searchView',
      name: 'SearchView',
      component: () => import('../views/SearchView.vue'),
      meta: { requiresAuth: true }
    },{
      path: '/ec',
      name: 'echarts',
      component: () => import('../views/echarts.vue'),
      meta: { requiresAuth: true }
    },
  ]
})

// 路由守卫
router.beforeEach((to, from, next) => {
  // 检查需要登录验证的路由
  if (to.meta.requiresAuth) {
    const token = localStorage.getItem('token')
    if (!token) {
      next('/login')
      return
    }
  }
  next()
})

export default router
